// Learn cc.Class:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html
// Learn Attribute:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html
// Learn life-cycle callbacks:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html

/*
 *修改密码界面 (修改提现密码，修改登录密码等)
*/
cc.Class({
    extends: cc.Component,

    properties: {
        labTop: cc.Label,  //标题
        labNumber: cc.Label, //手机号
        passWordCode: cc.EditBox, //若当前没有绑定手机号，要输入手机号    
        edCode: cc.EditBox,  //输入验证码
        labDec: cc.Label,  //获取验证码
        modifyCashSetPwdPrefab: cc.Prefab,  //重置密码预制
        _bgetVerity:true,
        _tel:"",
        _type:0,
        intervalTime: 60,

        _verify:"",
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        this._tel = User.getTel();
        if(this._tel != "")
        {
            var top = "*******";
            var down = this._tel.substring(7,11);
            this.labNumber.node.active = true;
            this.passWordCode.node.active = false;
            this.labNumber.string = top+down;
        }
        else
        {
            this._tel= "";
            this.labNumber.node.active = false;
            this.passWordCode.node.active = true;
        }   
    },

    init: function(type){
        this._type =type;
        if(this._type == 1)//登录密码修改
        {
            this.labTop.string = "修改登录密码";
        }
        else if(this._type == 2)//提现密码修改
        {
            this.labTop.string = "修改提现密码";
        }    
    },

    //获取验证码
    onGetVerifyCode: function(){
        if(this.passWordCode.node.active){
            if(this.passWordCode.string ==""){
                ComponentsUtils.showTips("请输入手机号!");
                return;
            }else{
                this._tel =this.passWordCode.string;
            }
        }

        if(!this._bgetVerity){
            return;
        }
        var self = this;
        var recv = function(ret){
            ComponentsUtils.unblock();
            if (ret.Code !== 0) {
                cc.error(ret.Msg);
                ComponentsUtils.showTips(ret.Msg);
            }else{
                self._bgetVerity = false;
                ComponentsUtils.showTips("发送成功!");   
                self.schedule(self._updateVerify, 1);
            }
        }.bind(this);
    
        var data = {
            Equipment: Utils.getEquipment(),
            Token: User.getLoginToken(),
            Mobile: this._tel,
            VerifyType:this._type==1? DEFINE.SHORT_MESSAGE.CHANGEPWD:DEFINE.SHORT_MESSAGE.CHANGEPAY,
        };
        CL.HTTP.sendRequest(DEFINE.HTTP_MESSAGE.GETVERIFY, data, recv.bind(this),"POST");  
        ComponentsUtils.block();
    },

    _updateVerify: function(){
        this.intervalTime = this.intervalTime-1;
        this.labDec.string = this.intervalTime+"秒后重发";
        if(this.intervalTime <= 0){
            this.intervalTime = 60;
            this._bgetVerity = true;
            this.unschedule(this._updateVerify);
            this.labDec.string = "获取验证码"
        }
    },

    //检查输入的验证码
    onTextChangedVerify:function(text, editbox, customEventData){
        var txt = text;
        if(Utils.isInt(txt) || txt == "")
            this._verify = txt;
        else
        {
            txt = this._verify;
            editbox.string = txt;
            return;
        }
    },

    checkInput:function(){
        if(this.edCode != "")
        {
            if(!Utils.isVerify(this.edCode.string))
            {
                ComponentsUtils.showTips("验证码格式不对！");
                return false;
            }
        }
        else
        {
            ComponentsUtils.showTips("验证码不能为空！");
            return false;
        }
        return true;
    },

    //下一步
    onNext: function(){
        if(this.passWordCode.node.active){
            if(this.passWordCode.string ==""){
                ComponentsUtils.showTips("请输入手机号!");
                return;
            }else{
                User.setTel(this.passWordCode.string);
            }
        }

        if(this.checkInput())
        {
            var recv = function(ret){
                ComponentsUtils.unblock();
                if (ret.Code !== 0) {
                    cc.error(ret.Msg);
                    ComponentsUtils.showTips(ret.Msg);
                }else{    
                    
                    var canvas = cc.find("Canvas");
                    var modifyCashSetPwdPrefab = cc.instantiate(this.modifyCashSetPwdPrefab);
                    if(this._type == 1)//登录密码修改
                    {
                        modifyCashSetPwdPrefab.getComponent("public_setPassword_pop").init({
                            Code:this.edCode.string,
                            type:this._type,
                        });
                    }
                    else if(this._type == 2)//提现密码修改
                    {
                        modifyCashSetPwdPrefab.getComponent("public_setPassword_pop").init({
                            Code:this.edCode.string,
                            type:this._type,
                        });
                    }
                    canvas.addChild(modifyCashSetPwdPrefab);
                    this.onClose();
                }
            }.bind(this);

            var data = {
                Token: User.getLoginToken(),
                Mobile: this._tel,
                VerifyCode: this.edCode.string,
                VerifyType:this._type==1? DEFINE.SHORT_MESSAGE.CHANGEPWD:DEFINE.SHORT_MESSAGE.CHANGEPAY,
            };
            CL.HTTP.sendRequestRet(DEFINE.HTTP_MESSAGE.CHECKVERIFY, data, recv.bind(this),"POST");  
            ComponentsUtils.block();
        }
    },
   

    onClose: function(){
        this.node.getComponent("Page").backAndRemove();
    },

    // update (dt) {},
});
